<!--
 * @Descripttion: 带客有礼列表
 * @Author: xiao li
 * @Date: 2020-07-06 12:17:07
 * @LastEditors: xiao li
 * @LastEditTime: 2021-04-29 14:05:22
-->
<template>
    <div class="lb-sys-sproconfig">
        <top-nav></top-nav>
        <div class="page-main">
          <el-row class="page-top-operate">
            <lb-button size='medium' type='primary' @click="$router.push('/app/present/edit')" icon='el-icon-plus'>添加福利</lb-button>
          </el-row>
        <div class="page-search-form">
          <el-form @submit.native.prevent :inline="true" :model="searchForm" ref="searchForm">
            <el-form-item label="标题" prop="title">
              <el-input v-model="searchForm.title" placeholder="请输入标题"></el-input>
            </el-form-item>
            <el-form-item>
              <lb-button size="medium" type="primary" icon="el-icon-search" style="margin-right:5px" @click="getTableDataList(1)">{{$t('action.search')}}</lb-button>
              <lb-button size="medium" icon="el-icon-refresh-left" style="margin-right:5px" @click="resetForm('searchForm')">{{$t('action.reset')}}</lb-button>
            </el-form-item>
          </el-form>
        </div>
            <el-table
            v-loading="loading"
            :data="tableData"
          :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
            style="width: 100%">
            <el-table-column
              prop="id"
              label="ID"
              width='100'>
            </el-table-column>
            <el-table-column
              prop="title"
              label="标题">
            </el-table-column>
            <el-table-column
              prop="user_coupon_data"
              label="新人福利">
            </el-table-column>
            <el-table-column
              prop="user_out_type"
              label="新人福利有效期">
              <template slot-scope="scope">
                <div v-if="scope.row.user_out_type === 1"><div>{{scope.row.user_coupon_start | handleTime}}</div> <div>{{scope.row.user_coupon_end | handleTime}}</div> </div>
                <div v-else>{{scope.row.user_out_day}}天</div>
              </template>
            </el-table-column>
            <el-table-column
              prop="coupon_data"
              label="推广人福利">
            </el-table-column>
            <el-table-column
              prop="out_type"
              label="推广人福利有效期">
              <template slot-scope="scope">
                <div v-if="scope.row.out_type === 1"><div>{{scope.row.coupon_start | handleTime}}</div><div>{{scope.row.coupon_end | handleTime}}</div> </div>
                <div v-else>{{scope.row.out_day}}天</div>
              </template>
            </el-table-column>
            <el-table-column
              prop="start_time"
              label="活动时间">
              <template slot-scope="scope">
                <div>{{scope.row.start_time | handleTime}}</div>
                <div>{{scope.row.end_time | handleTime}}</div>
              </template>
            </el-table-column>
            <el-table-column
              prop="create_time"
              label="创建时间">
              <template slot-scope="scope">
                <p>{{scope.row.create_time | handleTime(1)}}</p>
                <p>{{scope.row.create_time | handleTime(2)}}</p>
              </template>
            </el-table-column>
            <el-table-column
              label="操作">
                <template slot-scope="scope">
                  <div class="table-operate">
                    <!-- status_text 1未开始，2进行中，3已结束-->
                    <lb-button size='mini' plain type='primary' @click="$router.push('/app/present/edit?id=' + scope.row.id)">{{scope.row.status_text === 3 ? $t('action.view') : $t('action.edit')}}</lb-button>
                    <lb-button size='mini' plain type='danger' @click="toDel(2,scope.row.id)" v-if="scope.row.status_text !== 2">{{$t('action.delete')}}</lb-button>
                    <lb-button size='mini' plain type='danger' @click="toDel(1,scope.row.id)" v-if="scope.row.status_text === 2">结束活动</lb-button>
                    <lb-button size='mini' plain type='success' @click="$router.push('/app/present/welfare?id=' + scope.row.id)" v-if="scope.row.status_text !== 1">{{$t('menu.PresentWelfare')}}</lb-button>
                    <lb-button size='mini' plain type='warning' @click="$router.push('/app/present/record?id=' + scope.row.id)" v-if="scope.row.status_text !== 1">{{$t('menu.PresentRecord')}}</lb-button>
                  </div>
                </template>
            </el-table-column>
          </el-table>
          <lb-page
          :batch='false'
          :page='searchForm.page'
          :pageSize='searchForm.limit'
          :total='total'
          @handleSizeChange='handleSizeChange'
          @handleCurrentChange='handleCurrentChange'
          >
          </lb-page>
        </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      searchForm: {
        title: '',
        limit: 10,
        page: 1
      },
      total: 0,
      tableData: []
    }
  },
  activated () {
    this.getTableDataList()
  },
  methods: {
    resetForm (name) {
      this.$refs[name].resetFields()
      this.getTableDataList(1)
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    async getTableDataList (flag) {
      if (flag) this.searchForm.page = 1
      this.loading = true
      let {searchForm} = this
      let {code, data} = await this.$api.presentActiveList(searchForm)
      this.loading = false
      if (code !== 200) return
      this.tableData = data.data
      this.total = data.total
    },
    async toDel (type, id) {
      let confirmText = type === 1 ? this.$t('tips.confirmEndActive') : this.$t('tips.confirmDelete')
      this.$confirm(confirmText, this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.toConfirmDel(type, id)
      }).catch(() => {

      })
    },
    async toConfirmDel (type, id) {
      let method = type === 1 ? 'presentOverTime' : 'presentActiveDel'
      let {code} = await this.$api[method]({id})
      if (code !== 200) return
      this.$message.success(this.$t('tips.successOper'))
      if (type === 2) {
        this.searchForm.page = this.searchForm.page < Math.ceil((this.total - 1) / this.searchForm.limit) ? this.searchForm.page : Math.ceil((this.total - 1) / this.searchForm.limit)
      }
      this.getTableDataList()
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
    .el-input,
    .el-select{
      width: 200px;
    }
</style>
